package code.model

import org.squeryl.annotations.Column
import net.liftweb.record.field._
import net.liftweb.record._
import net.liftweb.squerylrecord.KeyedRecord
import net.liftweb.squerylrecord.RecordTypeMode._
import code.util.WebUtil


class Product private() extends Record[Product] with KeyedRecord[Long] with WebUtil {
  def meta = Product

  @Column(name="id")
  override val idField = new LongField(this)

  @Column(name="parent_id")
  val parentId = new IntField(this)

  @Column(name="art_id")
  val artId = new StringField(this,25)

  val price = new DoubleField(this)
  val title = new StringField(this,100)
  val sort = new IntField(this)
  val availability = new StringField(this,50)

  @Column(name="short_desc")
  val shortDesc = new StringField(this,100)

  @Column(name="long_desc")
  val longDesc = new StringField(this,2500)

  @Column(name="content_detail")
  val contentDetail = new TextareaField(this,2500)

  @Column(name="catalog_pic_height")
  val catalogPicHeight = new StringField(this,3)

  @Column(name="master_flag")
  val masterFlag = new BooleanField(this)

  def productImageSrc: String = {
    val paddedId:String = zeroPadId(idField.value)
    val subdir = paddedId.substring(0,2)
    val src = "/img/products/%s/%s.jpg".format(subdir,paddedId)

    src
  }

}

object Product extends Product with MetaRecord[Product] {
  def get(productId: Long): Option[Product] =
    MySchema.products.lookup(productId)

  def getByArtId(artId: String): Option[Product] = {
    MySchema.products.where(p => p.artId == artId).headOption
  }

  def getAllProducts: List[Product] =
    from(MySchema.products)(p => select(p)).toList

  def	getTotalProducts : Long =
    from(MySchema.products)(p => compute(count)).toLong
  

  //def getChildren
}